package cn.myjerry.test.reflection;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * 利用反射API，获取类的信息（名字、属性、构造器等）
 * @author Jerry
 *
 */
@SuppressWarnings("all")
public class Demo02 {
	public static void main(String[] args) throws Exception {
		String path = "cn.myjerry.test.reflection.bean.User";
		Class<?> clazz = Class.forName(path);
		
		// 获取类的名称
		System.out.println(clazz.getName());
		System.out.println(clazz.getSimpleName());
		
		// 获取属性信息
		//Field[] fields = clazz.getFields();	// 返回public的Field
		Field[] fields = clazz.getDeclaredFields();	// 返回所有的Field
		Field f = clazz.getDeclaredField("Name");
		System.out.println(fields.length);
		for(Field temp:fields) {
			System.out.println("属性： " + temp);
		}
		
		// 获取方法信息
		Method[] methods = clazz.getDeclaredMethods();
		Method m01 = clazz.getDeclaredMethod("getName", null);
		Method m02 = clazz.getDeclaredMethod("setName", String.class);
		for(Method m:methods) {
			System.out.println("方法：" + m);
		}
		
		// 获取构造器信息
		Constructor[] constructors = clazz.getDeclaredConstructors();
		Constructor c01 = clazz.getConstructor(int.class, int.class, String.class);
		System.out.println(c01);
		for(Constructor c:constructors) {
			System.out.println("构造器：" + c);
		}
		
	}
}
